<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>video</title>
    <style>
        html,
        body {
            margin: 0;
        }
        #btn {
            left: 50%;
            top: 50%;
            position: absolute;
            z-index: 100;
            width: 200px;
            height: 50px;
        }
    </style>
</head>

<body>
    <canvas class="webgl"></canvas>
    <script async src="../source/utils/es-module-shims.js"></script>

    <script type="importmap">
        {
            "imports": {
                "three": "../source/three.module.js",
                "OrbitControls.js": "../source/OrbitControls.js"
            }
        }
    </script>
    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'OrbitControls.js';

        const sizes = {
            width: window.innerWidth,
            height: window.innerHeight
        }

        const canvas = document.querySelector(".webgl");

        // 场景
        const scene = new THREE.Scene();

        // 相机
        const camera = new THREE.PerspectiveCamera(
            75,
            sizes.width / sizes.height,
            0.1,
            1000
        )
        camera.position.x = 0
        camera.position.y = 0
        camera.position.z = 0.01

        scene.add(camera);

        // 添加光照
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
        scene.add(ambientLight)

        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
        directionalLight.position.set(2, 2, -1)

        scene.add(directionalLight)

        // 控制器
        const controls = new OrbitControls(camera, canvas);
        controls.enablePan = false;
        controls.enableDamping = true;
        controls.rotateSpeed = - 0.25;

        scene.add(camera);

        /* 核心逻辑 */
        const geometry = new THREE.SphereGeometry(30, 64, 32);
        const material = new THREE.MeshBasicMaterial({ 
            // color: "red",
            map: new THREE.TextureLoader().load('./images/panorama/example.jpg'), transparent: true
        });

        const skyBox = new THREE.Mesh(geometry, material);
        material.side = THREE.DoubleSide;

        scene.add(skyBox);

        // camera.position.z = 100
         /* 核心结束 */


        window.addEventListener('resize', () => {
            // 设置大小
            sizes.width = window.innerWidth
            sizes.height = window.innerHeight

            // 更新相机
            camera.aspect = sizes.width / sizes.height
            camera.updateProjectionMatrix()

            // 更新渲染器
            renderer.setSize(sizes.width, sizes.height)
            renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
        })


        // 设置渲染器
        const renderer = new THREE.WebGLRenderer({
            canvas
        })
        // 设置大小
        renderer.setSize(sizes.width, sizes.height)
        // 设置像素比
        renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
        renderer.render(scene, camera);

        const tick = () => {
            controls.update();

            renderer.render(scene, camera)
            // texture.needsUpdate = true;

            window.requestAnimationFrame(tick)
        }

        tick();

    </script>
</body>

</html>